      subroutine Mbft(filename)
      implicit real*8 (a-h,o-z)
      character*40 fname,filename(20)
      logical filflg
      character*1 flg
      character*12 fname1,fname2
      common /aa/ ia(256000000)
c.... open disp0 file to get the numbers of nodes and degree of freedom
c.... knode .... number of nodes, kdgof .... number of d.o.f.
      open(1,file=filename(1),form='unformatted')
      read(1) knode,kdgof
      close(1)
      kvar=knode*kdgof
      KCOOR=3
      kna3=kdgof*knode*1
      if (kna3/2*2 .lt. kna3) kna3=kna3+1
      kna1=kcoor*knode*2
      kna2=kdgof*knode*2
      kna0=1
      kna1=kna1+kna0
      kna2=kna2+kna1
      kna3=kna3+kna2
      if (kna3-1.gt.256000000) then
      write(*,*) 'exceed memory of array ia'
      write(*,*) 'memory of ia = 256000000'
      write(*,*) 'memory needed = ',kna3,' in prgram bft'
      stop 55555
      endif
      call bft(knode,kdgof,kcoor,tmax,
     *time,it,ia(kna0),ia(kna1),ia(kna2),
     *filename)
C ...... OPEN THE FILE TO OBTAIN GRAPH FILE NAMES
      inquire(file='plotname',exist=filflg)
      if (.NOT. filflg) then
      fname1 = 'unod'
      open(6,file='plotname',form='formatted',status='unknown')
      write(6,'(8a)') fname1
      close(6)
      endif
      open(6,file='plotname',form='formatted',status='old')
C ...... OPEN THE BATCH FILE FOR STORING THE RESULT TO GRAPHIC
      open(7,file='post.bat',form='formatted',status='unknown')
      open(8,file='cpunod.bat',form='formatted',status='unknown')
C ...... STORE THE RESULT FOR EACH nstep TIME STEP
      nstep = 1
      ik = it/nstep
      kk = it-ik*nstep
cc      write(*,*) 'nstep,it,ik,kk =',nstep,it,ik,kk
      if (kk.gt.0) goto 9999
9998  CONTINUE
 
C ...... GET THE GRAPHIC FILE NAME
      read(6,'(8a)',END=9999) fname1
C       WRITE(*,*) 'fname1 =',fname1
      fname2 = fname1
      call bft_1(fname2,ik)
C ...... WRITE COPY COMMAND TO post.bat FILE FOR STORING THE RESULT
      write(7,*) 'copy ',fname1,' ',fname2
      fname2=fname1
      call bft_1(fname2,ik-1)
      write(8,*) 'copy ',fname2,' ',fname1
      GOTO 9998
9999  CONTINUE
      close(6)
      write(7,*)
      close(7)
      close(8)
c       write(*,*) 'it =',it
c       write(*,*) fname1
c       write(*,*) fname2
 
C.......OPEN STOP file IF THE LAST TIME IS ARRIVED
      IF (TIME-TMAX.GT.-1.0d-20) THEN
cc       OPEN(1,FILE=' ',FORM='UNFORMATTED',STATUS='unknown')
       open(16,file='plotno',form='formatted',status='unknown')
       write(16,*) 1,ik
       close(16)
      ENDIF
      return
      end
      subroutine bft(knode,kdgof,kcoor,tmax,
     *time,it,coor,bf,nodvar,
     *filename)
      implicit real*8 (a-h,o-z)
      character*40 filename(20)
      DIMENSION NODVAR(KDGOF,KNODE),COOR(KCOOR,KNODE),
     *  BF(KDGOF,KNODE),R(3)
      character*1 flg
C.......OPEN TIME File AND UPDATE THE TIME
cc      OPEN(1,FILE=' ',FORM='UNFORMATTED')
cc      READ(1) TMAX,DT,TIME,IT
      open(1,file='end',form='formatted')
      read(1,*) flg
      read(1,*) itm,itmax,istepmax,dtmin
      close(1)
      open(1,file='time0',form='formatted')
      read(1,*) t0,tm0,dt0
      close(1)

      OPEN(1,FILE=filename(2),FORM='UNFORMATTED')
      READ(1) TMAX,DT,TIME,IT
      if(it.gt.istepmax) then
      	write(*,*) 'incre_num:',it
      	write(*,*) 'IncrementMaxNum :',istepmax
      	write(*,*) 'err: incre_num gt IncrementMaxNum'
      	stop 5555
      endif
      if(flg.eq.'y') then
      	time=time-dt
      	it=it-1
      	dt=dt/4.0d0
      	if(dt.lt.dtmin) then
      		write(*,*) 'err: can not converge!!!'
         stop 0000
        endif
      elseif(itm.lt.itmax) then      	
      	if(it.ge.1.and.dt*1.5d0.le.0.3d0) dt=dt*1.5d0	
      	if(time+dt.ge.tmax) dt=tmax-time
      endif
      if(time+dt.ge.tmax) dt=tmax-time
      T = TIME+DT
      TIME = TIME+DT
      IT = IT+1
c      WRITE(*,*) ' TMAX,DT,TIME,IT =',TMAX,DT,TIME,IT
      REWIND(1)
      WRITE(1) TMAX,DT,TIME,IT
      CLOSE(1)
 
C.......OPEN COOR file
      OPEN (1,FILE=filename(3),FORM='UNFORMATTED',STATUS='OLD')
      READ (1) KNODE,NCOOR,((COOR(I,J),I=1,NCOOR),J=1,KNODE)
      CLOSE(1)
 
C.......OPEN NODVAR file
      OPEN (1,FILE=filename(4),FORM='UNFORMATTED',STATUS='OLD')
      READ (1) ((NODVAR(I,J),I=1,KDGOF),J=1,KNODE)
      CLOSE (1)
c      WRITE(*,*) 'KNODE =',KNODE,' KDGOF =',KDGOF
C     WRITE (*,*) 'NODVAR ='
C     WRITE (*,6) ((NODVAR(I,J),I=1,KDGOF),J=1,KNODE)
 
C......COMPUTE BOUNDARY CONDITION
      DO 333 N=1,KNODE
      DO 100 J=1,NCOOR
100   R(J) = COOR(J,N)
      DO 200 J=1,KDGOF
      ID = NODVAR(J,N)
      BF(J,N) = 0.0
c      IF (ID.LT.0) BF(J,N) = BOUND(R,T,J)
C     IF (ID.GT.0) BF(J,N) = FORCE(R,T,J)
200   CONTINUE
333   CONTINUE
c      WRITE(*,*) ' BF = '
c      WRITE(*,'(6F13.3)') ((BF(J,N),J=1,KDGOF),N=1,KNODE)
 
C.......OPEN BFD file and WRITE BOUNDARY CONDITION
      OPEN (1,FILE=filename(5),FORM='UNFORMATTED',STATUS='unknown')
      WRITE(1) ((BF(I,J),I=1,KDGOF),J=1,KNODE)
      CLOSE (1)
 
C.......OPEN STOP file IF THE LAST TIME IS ARRIVED
      IF (TIME-TMAX.GT.-1.0d-20) THEN
      OPEN(1,FILE=filename(6),FORM='UNFORMATTED',STATUS='unknown')
      ENDIF
      END
 
      subroutine bft_1(name,IT)
      implicit real*8 (a-h,o-z)
      character name*12,ch3*3
c     IF (IT.LT.10) WRITE(UNIT=CH3,FMT='(I1)') IT
c     IF (IT.GE.10) WRITE(UNIT=CH3,FMT='(I2)') IT
c     IF (IT.GE.100) WRITE(UNIT=CH3,FMT='(I3)') IT
      call bft_2(it,ch3)
c     write(*,*) 'name =',name,'++++ CH3 =',CH3
      do 10 i=1,12
      if (name(i:i).eq.' ') then
      j=i
      goto 20
      endif
10    continue
20    continue
      if (j.gt.9) then
      write(*,*) 'Error, plot filename too long .......',name
      write(*,*) ' the length of name must be less or equal 8 character'
      stop 111
      endif
c     read(*,'(a3)') ch3
      name(j:j)='.'
      name(j+1:j+4)=ch3
c     write(*,*) 'plot filename = ',name
      return
      end
 
      subroutine bft_2(ii,ch3)
      implicit real*8 (a-h,o-z)
      character ch3*3
      it = ii
      ch3 = '   '
      k = 0
      if (ii.ge.100) then
      n = it/100
      k = k+1
      call bft_3(n,k,ch3)
      it = it - n*100
      endif
      if (ii.ge.10) then
      n = it/10
      k = k+1
      call bft_3(n,k,ch3)
      it = it - n*10
      endif
      n = it
      k = k+1
      call bft_3(n,k,ch3)
      return
      end
 
      subroutine bft_3(n,k,ch3)
      implicit real*8 (a-h,o-z)
      character ch3*3
      if (n.eq.0) ch3(k:k) = '0'
      if (n.eq.1) ch3(k:k) = '1'
      if (n.eq.2) ch3(k:k) = '2'
      if (n.eq.3) ch3(k:k) = '3'
      if (n.eq.4) ch3(k:k) = '4'
      if (n.eq.5) ch3(k:k) = '5'
      if (n.eq.6) ch3(k:k) = '6'
      if (n.eq.7) ch3(k:k) = '7'
      if (n.eq.8) ch3(k:k) = '8'
      if (n.eq.9) ch3(k:k) = '9'
      return
      end
